/* =============================================================
 * date-utils.js v1.1.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 15/06/2021
 * ============================================================ */


define(["jquery", "CommonUtils", "StringUtils", "NumberUtils"], function ($, CommonUtils, StringUtils, NumberUtils) {
    let ArrayUtils = {
        isEmpty: function (array) {
            let boolean = false;

            if (!array || Object.is(Object.keys(array).length, 0)) boolean = true;

            return boolean;
        },

        /**
         * 单一数组去重  ["1","2","2","3"]
         * @param array
         * @returns {any[]|*[]}
         */
        noRepeatSingle: function (array) {
            if (!CommonUtils.isArray(array)) return [];

            return Array.from(new Set(array));
        },

        /**
         * 数组按某一属性分组
         * @param array
         * @param key
         * @returns {*[]}
         */
        groupBy: function (array, key) {
            // key:数组对象中的属性 此方法返回值是一个对象
            if (!CommonUtils.isArray(array)) return [];

            let group = array.reduce((acc, cur) => {
                (acc[cur[key]] = acc[cur[key]] || []).push(cur);
                return acc;
            }, {});
            // 整理成数组
            let arr = [];
            Object.keys(group).forEach((key) => {
                arr.push({
                    key: key,
                    list: group[key],
                });
            });

            return arr;
        },

        /**
         * 数组 按照某一属性求和
         * @param array
         * @param key
         * @returns {*[]|*}
         */
        sumByKey: function (array, key) {
            if (!CommonUtils.isArray(array)) return [];

            return array.reduce((acc, cur, idx, src) => {
                // acc:上一次调用回调时返回的累积值  cur:正在处理的元素  idx:正在处理的当前元素的索引  src:源数组(原数组)
                return NumberUtils.addition(acc, cur[key]);
            }, 0);
        },

        /**
         * 多维数组转一维数组
         * @param array
         * @returns {*[]|*}
         */
        transferOneArray: function (array) {
            // let arr = [[0, 1], [2, 3], [4,[5,6,7]]]
            if (!Array.isArray(array)) return [];

            return array.reduce(
                (acc, cur) =>
                    acc.concat(Array.isArray(cur) ? this.transferOneArray(cur) : cur),
                []
            );
        },

        /**
         * 记录数组中元素重复次数
         * @param array
         * @returns {*}
         */
        countEleRepeatTime: function (array) {
            // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
            return array.reduce((acc, cur) => {
                if (cur in acc) {
                    acc[cur]++;
                } else {
                    acc[cur] = 1;
                }
                return acc;
            }, {});
        },

        /**
         * 按照汉字和首字母分组
         * @param arr
         * @returns {null|*[]}
         */
        groupByCharacters: function (arr) {
            // 数据格式  let items = [{id: "1",name: "安吉",}, {id: "2",name: "安庆",}, {id: "3",name: "大连",}, {id: "4",name: "上海",}, {id: "5",name: "北京",}];
            if (!String.prototype.localeCompare) return null;

            let letters = "*abcdefghjklmnopqrstwxyz".split("");
            let zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split("");

            let segs = [];
            let curr;
            letters.forEach((item, i) => {
                curr = {letter: item, data: []};
                arr.forEach((itm) => {
                    if (
                        (!zh[i - 1] || zh[i - 1].localeCompare(itm.name) <= 0) &&
                        itm.name.localeCompare(zh[i]) == -1
                    ) {
                        curr.data.push(itm);
                    }
                });
                if (curr.data.length) {
                    segs.push(curr);
                    curr.data.sort(function (a, b) {
                        return a.name.localeCompare(b.name);
                    });
                }
            });
            return segs;
        },

        /**
         * 按照汉字顺序排序
         * @param array
         * @returns {*}
         */
        orderByCharacters: function (array) {
            let resultArray = array.sort(function (a, b) {
                return a.name.localeCompare(b.name, "zh");
            });

            return resultArray;
        },

        /**
         * 按照数字顺序排序
         * @param array
         * @param key
         * @param mode
         * @returns {boolean}
         */
        orderByNumber: function (array, key, mode) {
            // key：数组中对象的属性  mode：up-升序  down-降序
            mode = mode ? mode : "up";
            let resultArray = array.sort((a, b) => {
                if (StringUtils.equals("up", mode)) {
                    return a[key] - b[key];
                } else {
                    return b[key] - a[key];
                }
            });

            return resultArray;
        },

        /**
         * 根据数组对象的某个属性去重  [{name:1},{name:2}]
         * @param array
         * @param val
         * @returns {*}
         */
        noRepeatByProp: function (array, prop) {
            const res = new Map();
            return array.filter(item => !res.has(item[prop]) && res.set(item[prop], 1))
        },

        /**
         * 两个对象型数组  按照某一属性排重  [{id:1,age:1},{id:2,age:1}]   [{id:1,age:3}]
         * @param array
         * @param val
         * @returns {*}
         */
        noRepeatsByProp: function (array1, array2, prop) {
            let propList = array1.map(item => item[prop]);
            let array = array2.filter(item => !propList.includes(item[prop]));

            return array1.concat(array);
        },
    };

    return ArrayUtils;
});
